package com.hornsun.repository;

import com.google.common.base.Preconditions;
import com.hornsun.util.StrUtil;
import com.hornsun.domain.dbo.OrderForm;
import com.hornsun.domain.dbo.OrderFormExample;
import com.hornsun.repository.mapper.OrderFormMapper;
import com.hornsun.util.LittleUtil;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 编译中... on 2018/3/28.
 */
@Repository
public class OrderFormRepository {

    @Resource
    private OrderFormMapper orderFormMapper;

    @Resource
    private LittleUtil littleUtil;

    /**
     * 插入订单
     * @param orderForm
     */
    public void insertOrderForm(OrderForm orderForm){
        int count = orderFormMapper.insertSelective(orderForm);
        if (count!=1){
            throw new IllegalArgumentException("insert orderForm failed by orderForm:"+orderForm);
        }
    }

    /**
     * 修改订单
     * @param orderForm
     */
    public void updateOrderForm(OrderForm orderForm){
        int count = orderFormMapper.updateByPrimaryKeySelective(orderForm);
        if(count!=1){
            throw new IllegalArgumentException("update orderForm failed by orderForm:"+orderForm);
        }
    }

    /**
     * 删除订单
     * @param guid
     */
    public void deleteOrderForm(String guid){
        int count = orderFormMapper.deleteByPrimaryKey(guid);
        if(count!=1){
            throw new IllegalArgumentException("delete orderForm failed by guid:"+guid);
        }
    }

    /**
     * 分页获取个人订单
     * @param openid
     * @param page
     * @return
     */
    public List<OrderForm> getOrderForm(String openid,int page){
        OrderFormExample orderFormExample = new OrderFormExample();
        OrderFormExample.Criteria criteria = orderFormExample.createCriteria();
        criteria.andOpenidEqualTo(openid);
        orderFormExample.setOrderByClause(StrUtil.toDesc("order_time"));
        return orderFormMapper.selectByExampleWithRowbounds(orderFormExample,littleUtil.littleRowBounds(page));
    }


    /**
     * 分页并按订单状态获取个人订单
     * @param openid
     * @param type
     * @param page
     * @return
     */
    public List<OrderForm> getOrderFormByType(String openid,int type,int page){
        OrderFormExample orderFormExample = new OrderFormExample();
        orderFormExample.setOrderByClause(StrUtil.toDesc("order_time"));
        OrderFormExample.Criteria criteria = orderFormExample.createCriteria();
        criteria.andOpenidEqualTo(openid).andTransactionStatusEqualTo((byte)type);
        return orderFormMapper.selectByExampleWithRowbounds(orderFormExample,littleUtil.littleRowBounds(page));
    }

    /**
     * 通过guid获取订单
     * @param orderGuid
     * @return
     */
    public OrderForm getOrderByOrderGuid(String orderGuid){
        return orderFormMapper.selectByPrimaryKey(orderGuid);
    }


    /**
     * 修改订单状态
     * @param orderGuid
     * @param status
     */
    public void updateOrderStatus(String orderGuid,byte status){
        Preconditions.checkNotNull(orderGuid);
        Preconditions.checkNotNull(status);
        orderFormMapper.updateOrderStatus(orderGuid,status);
    }

    /**
     * 获取当天订单数量
     * @return
     */
    public long todayOrder(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = sdf.format(date)+"%";
        return orderFormMapper.getTodayOrder(timeStr);
    }

    /**
     * 分页获取所有订单
     * @param page
     * @return
     */
    public List<OrderForm> getOrders(int page){
        OrderFormExample orderFormExample = new OrderFormExample();
        orderFormExample.setOrderByClause(StrUtil.toDesc("order_time"));
        return orderFormMapper.selectByExampleWithRowbounds(orderFormExample,littleUtil.littleRowBounds(page));
    }

    /**
     * 分页按状态获取所有订单
     * @param type
     * @param page
     * @return
     */
    public List<OrderForm> getOrdersByType(int type,int page){
        OrderFormExample orderFormExample = new OrderFormExample();
        orderFormExample.setOrderByClause(StrUtil.toDesc("order_time"));
        OrderFormExample.Criteria criteria = orderFormExample.createCriteria();
        criteria.andTransactionStatusEqualTo((byte)type);
        return orderFormMapper.selectByExampleWithRowbounds(orderFormExample,littleUtil.littleRowBounds(page));
    }

    /**
     * 获得订单总条数
     * @return
     */
    public long getTotalCount(){
        return orderFormMapper.countByExample(new OrderFormExample());
    }

    /**
     * 获得某状态的订单数目
     * @param type
     * @return
     */
    public long getCountByType(int type){
        OrderFormExample orderFormExample = new OrderFormExample();
        OrderFormExample.Criteria criteria = orderFormExample.createCriteria();
        criteria.andTransactionStatusEqualTo((byte)type);
        return  orderFormMapper.countByExample(orderFormExample);
    }

    public List<OrderForm> getByAlias(String alias){
        OrderFormExample orderFormExample = new OrderFormExample();
        OrderFormExample.Criteria criteria = orderFormExample.createCriteria();
        criteria.andAliasEqualTo(alias);
        return orderFormMapper.selectByExample(orderFormExample);
    }

}

